/**
 * 注册停车场信息之后进行数据库存储
 */
package controller;


import hibernate.Adminstab;
import hibernate.AdminstabDAO;
import hibernate.Parkstab;
import hibernate.ParkstabDAO;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class ParkRegistSuccess implements Action {
	private String name;
	private String telNumber;
	private int totalSpace;
	private int emptySpace;
	private String description;
	private String address;
	private double price;
	private int maxOrderTime;
	private double longitude;
	private double latitude;
	private Parkstab parkstab;

	
	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getTelNumber() {
		return telNumber;
	}


	public void setTelNumber(String telNumber) {
		this.telNumber = telNumber;
	}


	public int getTotalSpace() {
		return totalSpace;
	}


	public void setTotalSpace(int totalSpace) {
		this.totalSpace = totalSpace;
	}


	public int getEmptySpace() {
		return emptySpace;
	}


	public void setEmptySpace(int emptySpace) {
		this.emptySpace = emptySpace;
	}


	public String getDescription() {
		return description;
	}


	public void setDescription(String description) {
		this.description = description;
	}


	public String getAddress() {
		return address;
	}


	public void setAddress(String address) {
		this.address = address;
	}


	public double getPrice() {
		return price;
	}


	public void setPrice(double price) {
		this.price = price;
	}


	public int getMaxOrderTime() {
		return maxOrderTime;
	}


	public void setMaxOrderTime(int maxOrderTime) {
		this.maxOrderTime = maxOrderTime;
	}


	public double getLongitude() {
		return longitude;
	}


	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}


	public double getLatitude() {
		return latitude;
	}


	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}


	public String execute(){
		ParkstabDAO parkstabDAO=new ParkstabDAO();
		parkstabDAO.getSession().beginTransaction();
		try
		{
			parkstab = new Parkstab();
			parkstab.setName(getName());
			parkstab.setTelNumber(getTelNumber());
			parkstab.setTotalSpace(getTotalSpace());
			parkstab.setEmptySpace(getEmptySpace());
			parkstab.setDescription(getDescription());
			parkstab.setAddress(getAddress());
			parkstab.setPrice(getPrice());
			parkstab.setMaxOrderTime(getMaxOrderTime());
			parkstab.setLongitude(getLongitude());
			parkstab.setLatitude(getLatitude());
			//ParkstabDAO parkstabDAO = new ParkstabDAO();
			//Parkstab parkstab = parkstabDAO.findById(getParkUUID());
			//adminstab.setParkstab(parkstab);

			parkstabDAO.attachDirty(parkstab);
			parkstabDAO.getSession().getTransaction().commit();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			parkstabDAO.getSession().close();
		}
		System.out.println("车场添加成功");
		
		//修改管理员的停车场外键
		
		AdminstabDAO adminstabDAO=new AdminstabDAO();
		adminstabDAO.getSession().beginTransaction();
		ActionContext ctx = ActionContext.getContext();
		try
		{
			Adminstab adminstab = (Adminstab)adminstabDAO.findByAccount(ctx.getSession().get("account")).get(0);
			adminstab.setParkstab(parkstab);
			ctx.getSession().put("parkID", parkstab.getUuid());
			
			//ParkstabDAO parkstabDAO = new ParkstabDAO();
			//Parkstab parkstab = parkstabDAO.findById(getParkUUID());
			//adminstab.setParkstab(parkstab);

			adminstabDAO.getSession().update(adminstab);
			adminstabDAO.getSession().getTransaction().commit();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			adminstabDAO.getSession().close();
		}


		System.out.println("管理员停车场添加成功");


		

		return SUCCESS;
	}
}
